# -*- coding: utf-8 -*-
"""
Created on Wed Aug 14 16:08:56 2019

@author: HCC
"""
import pandas as pd
import numpy as np
import pickle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.linear_model import LogisticRegressionCV
import statsmodels.api as sm
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
import scorecardpy as sc


gt1 = pd.read_csv('F:/data/GT1.csv', header = 0) #load the data

print(gt1.shape)

dt_s = sc.var_filter(gt1, y = "bad1_flag", iv_limit=0.02) 

dt_s_clean = dt_s.drop(['手机品牌','机型'], axis = 1)

# test_size 训练集与测试集比例，训练集和测试集是随机区分的，当设定random_state的状态后，后期做复盘具有依据
trainData, testData = train_test_split(dt_s_clean,test_size=0.3,random_state =22)

x = dt_s_clean.iloc[:,:213]
y = dt_s_clean["bad1_flag"]




def KS_AR(df, score, target): 
    total = df.groupby([score])[target].count() 
    bad = df.groupby([score])[target].sum() 
    all = pd.DataFrame({'total':total, 'bad':bad}) 
    all['good'] = all['total'] - all['bad'] 
    all[score] = all.index 
    all = all.sort_values(by=score,ascending=False) 
    all.index = range(len(all)) 
    all['badCumRate'] = all['bad'].cumsum() / all['bad'].sum() 
    all['goodCumRate'] = all['good'].cumsum() / all['good'].sum() 
    all['totalPcnt'] = all['total'] / all['total'].sum() 
    arList = [0.5 * all.loc[0, 'badCumRate'] * all.loc[0, 'totalPcnt']] 
    for j in range(1, len(all)): 
        ar0 = 0.5 * sum(all.loc[j - 1:j, 'badCumRate']) * all.loc[j, 'totalPcnt'] 
        arList.append(ar0) 
        arIndex = (2 * sum(arList) - 1) / (all['good'].sum() * 1.0 / all['total'].sum()) 
    KS = all.apply(lambda x: x.badCumRate - x.goodCumRate, axis=1) 
    return {'AR':arIndex, 'KS': max(KS)}